Skip to content
This repository was archived by the owner on Oct 16, 2025. It is now read-only.

fix: reject pending getLatestBlock requests when block tracker is stopped#320

Merged
cryptodev-2s merged 2 commits intomainfrom
fix/reject-pending-latest-block-on-stop
May 28, 2025
Merged

fix: reject pending getLatestBlock requests when block tracker is stopped#320
cryptodev-2s merged 2 commits intomainfrom
fix/reject-pending-latest-block-on-stop

Conversation

@cryptodev-2s
Copy link
Copy Markdown
Contributor

Description

Fixes a bug where getLatestBlock() promises could hang indefinitely when the block tracker is stopped before the request completes.

Reference

Fixes #319

Problem

The #pendingLatestBlock private instance variable tracks pending requests for the latest block. However, when the polling loop is stopped (via removeAllListeners() or similar), any pending getLatestBlock() promises would never resolve, causing:

  • Memory leaks from unresolved promises
  • Poor error handling for applications
  • Inconsistent behavior compared to the destroy() method

Solution

Modified the _maybeEnd() method to call #rejectPendingLatestBlock() when the block tracker is stopped, ensuring that any pending getLatestBlock() requests are properly rejected with a "Block tracker destroyed" error.

Steps to Reproduce the Bug

  1. Start the block tracker
  2. Wait for the first block update to resolve
  3. Immediately call getLatestBlock()
  4. Immediately stop the block tracker
  5. The call to getLatestBlock() would hang forever ❌

After Fix

The getLatestBlock() promise is now properly rejected with "Block tracker destroyed" error ✅

@cryptodev-2s cryptodev-2s self-assigned this May 28, 2025
@cryptodev-2s cryptodev-2s requested a review from a team as a code owner May 28, 2025 14:31
@cryptodev-2s cryptodev-2s force-pushed the fix/reject-pending-latest-block-on-stop branch from 11f01ed to 2d8f3ba Compare May 28, 2025 14:34
// play: one to go to the next iteration of the block tracker
// loop, another to expire the current block number cache. We don't
// know which one has been added first, so we have to find it.
blockTracker.removeAllListeners();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to restart the block tracker to clear the cached currentBlock.

Comment on lines +888 to +901
// Verify that the block reset timeout is set up
expect(
setTimeoutRecorder.calls.some((call) => {
return call.duration === blockTrackerOptions.blockResetDuration;
}),
).toBe(true);

// Wait for the block reset timeout to complete
await setTimeoutRecorder.nextMatchingDuration(
blockTrackerOptions.blockResetDuration,
);

// Verify that the current block is still null after the timeout
expect(blockTracker.getCurrentBlock()).toBeNull();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really tied to the test and thus can be removed

@cryptodev-2s cryptodev-2s force-pushed the fix/reject-pending-latest-block-on-stop branch from 2d8f3ba to acce3c3 Compare May 28, 2025 14:41
Copy link
Copy Markdown
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@cryptodev-2s cryptodev-2s merged commit 3fe05d9 into main May 28, 2025
9 checks passed
@cryptodev-2s cryptodev-2s deleted the fix/reject-pending-latest-block-on-stop branch May 28, 2025 15:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reject pending fetch when block tracker is stopped

2 participants